const router = require('koa-router')();
router.prefix('/dqk-data-sys/sys_user');
const SysUserService = appRequire('service/desktop/SysUserService.js');
const SysRoleService = appRequire('service/desktop/SysRoleService.js');
const ResultVo = appRequire('vo/ResultVo.js');

router.get('/index',async(ctx)=>{
  ctx.body="sys_user 你好";
});
router.post('/login',async(ctx)=>{
  try{
    console.log(ctx.request.body);
    let user = ctx.request.body;
    let dbUser = await SysUserService.findOneByUsernameAndPassword(user);
    console.log("dbUser:"+JSON.stringify(dbUser));
    if(dbUser){
      if(dbUser.username===user.username&&dbUser.password===user.password){
        user.accountUsername = user.username;
        user.password = "";
        user.username = "";
        ctx.session.user = {id:dbUser.id,username:dbUser.username};
        ctx.body = ResultVo.success(user.accountUsername+'登陆成功!',user);
      }else{
        ctx.body=ResultVo.error(-1,'error','登录失败!');
      }
    }
    else{
      ctx.body=ResultVo.error(-1,'error','登录失败!');
    }
  }catch(err){
    //集中处理错误
    err.msg = "APP登录验证失败!";
    ctx.app.emit('error', err, ctx);
  }
});
module.exports = router;